﻿var request = require('request');
var crypto = require("crypto");
var qs = require('querystring');
var log = require('./logger.js');
var config = require('../config.js');


var tools = {

    push: function (template_id, openid, content, arr, remark) {

        return new Promise((resolve, reject) => {

            var ReqURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + config.wxgzh.appid + "&secret=" + config.wxgzh.secret;
            request({
                url: ReqURL,
                method: 'GET',
                headers: {
                    "content-type": "application/x-www-form-urlencoded"
                },
            }, function (error, response, body) {
                // console.log('error:', error); // Print the error if one occurred
                //console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
                // console.log('body:', body);
                if (!error && response.statusCode == 200) {
                    var json = JSON.parse(body);
                    var access_token = json.access_token;
                    console.log("access_token" + access_token);

                    var template = {
                        touser: openid,
                        template_id: template_id,
                        url: "",
                        data: {
                            first: {
                                value: content,
                                color: "#000000"
                            },
                            keyword1: {
                                value: arr[0],
                                color: "#1d1d1d"
                            },
                            keyword2: {
                                value: arr[1],
                                color: "#000000"
                            },
                            keyword3: {
                                value: arr[2],
                                color: "#000000"
                            },
                            remark: {
                                value: remark,
                                color: "#000000"
                            }
                        }
                    };
                    console.log(JSON.stringify(template));
                    request({
                        url: "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token,
                        method: 'POST',
                        body: JSON.stringify(template),
                        headers: {
                            "content-type": "application/x-www-form-urlencoded"
                        },
                    }, function (error, response, body) {

                        var result = {
                            "status": 200,
                            "message": "success",
                            "data": body
                        }
                        resolve(result)

                    });




                } else {
                    var result = {
                        "status": 400,
                        "message": "error",
                        "data": ""
                    }
                    resolve(result)
                }
            });

        });


    }

}


module.exports = tools;